package com.xiguaji.tiktok.task.blogger;

import com.xiguaji.boot.data.rdb.aggregate.DistributedAggregate;
import com.xiguaji.tiktok.task.blogger.spider.model.User;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Blogger extends DistributedAggregate {
    /**
     * o_avatar_300x300
     */
    private String oAvatar300;

    /**
     * o_uid
     */
    private Long oUid;

    /**
     * o_avatar_medium
     */
    private String oAvatarMedium;

    /**
     * 被关注数
     */
    private Integer oFollowerCount;

    /**
     * 区域
     */
    private String oDistrict;

    /**
     * 签名
     */
    private String oSignature;

    /**
     * 平台关注数
     */
    private Integer oMplatformFollowersCount;

    /**
     * o_cover_url
     */
    private String oCoverUrl;

    /**
     * o_is_star
     */
    private Boolean oIsStar;

    /**
     * 主动关注数
     */
    private Integer oFollowingCount;

    /**
     * 总点赞数
     */
    private Long oTotalFavorited;

    /**
     * o_dongtai_count
     */
    private Integer oDongtaiCount;

    /**
     * o_avatar_168x168
     */
    private String oAvatar168;

    /**
     * 性别
     */
    private Integer oGender;

    /**
     * o_share_info
     */
    private String oShareInfo;

    /**
     * o_avatar_larger
     */
    private String oAvatarLarger;

    /**
     * o_birthday
     */
    private String oBirthday;

    /**
     * o_country
     */
    private String oCountry;

    /**
     * o_city
     */
    private String oCity;

    /**
     * o_is_activity_user
     */
    private Boolean oIsActivityUser;

    /**
     * o_avatar_thumb
     */
    private String oAvatarThumb;

    /**
     * o_province
     */
    private String oProvince;

    /**
     * o_aweme_count
     */
    private Integer oAwemeCount;

    /**
     * o_location
     */
    private String oLocation;

    /**
     * o_sec_uid
     */
    private String oSecUid;

    /**
     * o_nickname
     */
    private String oNickname;

    /**
     * o_room_id
     */
    private Long oRoomId;

    /**
     * o_region
     */
    private String oRegion;

    /**
     * o_video_icon
     */
    private String oVideoIcon;

    private String oUniqueId;

    private int oConstellation;

    private String oEnterpriseVerifyReason;

    private boolean oWithFusionShopEntry;

    @Mapper(componentModel = "spring")
    public interface BloggerMapper {
        @Mapping(target = "oAvatar300", expression = "java(dto.getAvatar_300x300().getUrl_list().get(0))")
        @Mapping(target = "oUid", source = "uid")
        @Mapping(target = "oAvatarMedium", expression = "java(dto.getAvatar_medium().getUrl_list().get(0))")
        @Mapping(target = "oFollowerCount", source = "follower_count")
        @Mapping(target = "oDistrict", source = "district")
        @Mapping(target = "oSignature", source = "signature")
        @Mapping(target = "oMplatformFollowersCount", source = "mplatform_followers_count")
        @Mapping(target = "oCoverUrl", expression = "java(dto.getCover_url().get(0).getUrl_list().get(0))")
        @Mapping(target = "oIsStar", source = "is_star")
        @Mapping(target = "oFollowingCount", source = "following_count")
        @Mapping(target = "oTotalFavorited", source = "total_favorited")
        @Mapping(target = "oDongtaiCount", source = "dongtai_count")
        @Mapping(target = "oAvatar168", expression = "java(dto.getAvatar_168x168().getUrl_list().get(0))")
        @Mapping(target = "oGender", source = "gender")
        @Mapping(target = "oShareInfo", expression = "java(getShare_info(dto))")
        @Mapping(target = "oAvatarLarger", expression = "java(dto.getAvatar_larger().getUrl_list().get(0))")
        @Mapping(target = "oBirthday", source = "birthday")
        @Mapping(target = "oCountry", source = "country")
        @Mapping(target = "oCity", source = "city")
        @Mapping(target = "oIsActivityUser", source = "is_activity_user")
        @Mapping(target = "oAvatarThumb", expression = "java(dto.getAvatar_thumb().getUrl_list().get(0))")
        @Mapping(target = "oProvince", source = "province")
        @Mapping(target = "oAwemeCount", source = "aweme_count")
        @Mapping(target = "oLocation", source = "location")
        @Mapping(target = "oSecUid", source = "sec_uid")
        @Mapping(target = "oNickname", source = "nickname")
        @Mapping(target = "oRoomId", source = "room_id")
        @Mapping(target = "oRegion", source = "region")
        @Mapping(target = "oVideoIcon", expression = "java(getVideo_icon(dto))")
        @Mapping(target = "oUniqueId", source = "unique_id")
        @Mapping(target = "oConstellation", source = "constellation")
        @Mapping(target = "oEnterpriseVerifyReason", source = "enterprise_verify_reason")
        @Mapping(target = "oWithFusionShopEntry", source = "with_fusion_shop_entry")
        Blogger mapper(User dto);

        List<Blogger> mapper(List<User> dtos);

        default String getVideo_icon(User dto) {
            if (dto.getVideo_icon() == null || CollectionUtils.isEmpty(dto.getVideo_icon().getUrl_list())) {
                return "";
            }
            return dto.getVideo_icon().getUrl_list().get(0);
        }

        default String getShare_info(User dto) {
            return dto.getShare_info() == null ? "" : dto.getShare_info().getShare_url();
        }
    }
}
